#include "mainwindowkeyboard.h"
#include "ui_mainwindowkeyboard.h"

extern QLineEdit *le; // 外部声明

MainWindowKeyboard::MainWindowKeyboard(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MainWindowKeyboard)
{
    ui->setupUi(this);

    QString keys = "!-@-#-$-%-^-&-*-(-)-1-2-3-4-5-6-7-8-9-0-Q-W-E-R-T-Y-U-I-O-P-A-S-D-F-G-H-J-K-L-:-Z-X-C-V-B-N-M-,-<=-关闭";
    QStringList keylist = keys.split(QRegExp("-"));
    int row = 0;
    int col = 0;

    for (int i = 0; i < keylist.count(); ++i)
    {
        QPushButton *b = new QPushButton(keylist[i]);
        b->setFixedSize(30, 30);
        connect(b, &QPushButton::clicked, this, &MainWindowKeyboard::key_clicked);
        ui->gridLayout->addWidget(b, row, col);

        ++ col;
        if (col == 10)
        {
            col = 0;
            ++row;
        }
    }
}

MainWindowKeyboard::~MainWindowKeyboard()
{
    delete ui;
}

void MainWindowKeyboard::key_clicked()
{
    QPushButton *button = qobject_cast<QPushButton*>(sender());
    if (!button)
        return;

    QString key = button->text();

    if (key == "<=")
    {
        if (le)
            le->backspace();
    }
    else if (key == "关闭")
    {
        this->close();
    }
    else
    {
        if (le)
            le->setText(le->text() + key);
    }
}

void MainWindowKeyboard::on_btn_ok_clicked()
{
    this->hide();
}
